import type { GenTableColumn, TableInfo } from "@/pages/tool/gen/entity";
import type React from "react";
import type { Result } from "#/api";
import apiClient from "../../apiClient";

export interface PageListReq {
	tableName?: string;
	tableComment?: string;
	page: number;
	pageSize: number;
}
export interface PageListRes {
	total: number;
	records: TableInfo[];
}

const pageList = (data: PageListReq) => apiClient.get<PageListRes>({ url: "/tool/gen/list", params: data });
const getInfo = (id: React.Key) => apiClient.get<TableInfo>({ url: `/tool/gen/${id}` });
const add = (data: TableInfo) => apiClient.post<TableInfo>({ url: "/tool/gen/add", data });
const update = (data: TableInfo) => apiClient.put<TableInfo>({ url: "/tool/gen", data });
const deleteById = (ids: React.Key[]) => apiClient.delete({ url: "/tool/gen", params: { ids: ids.join(",") } });

//查询数据库表信息
const dbList = (data: PageListReq) => apiClient.get<PageListRes>({ url: "/tool/gen/db/list", params: data });
//获取表字段
const columnList = (id: React.Key) => apiClient.get<GenTableColumn[]>({ url: `/tool/gen/column/${id}` });
//导入数据库表信息
const importTable = (data: any) => apiClient.post<Result<any>>({ url: "/tool/gen/importTable", params: data });
//预览生成的信息
const preview = (id: React.Key) => apiClient.get<Result<any>>({ url: `/tool/gen/preview/${id}` });
//生成代码（下载方式）
const download = (tableName: React.Key) => apiClient.get<Result<any>>({ url: `/tool/gen/download/${tableName}` });
//生成代码（自定义路径）
const genCode = (tableName: React.Key) => apiClient.get<Result<any>>({ url: `/tool/gen/genCode/${tableName}` });
//同步数据库
const synchDb = (tableName: React.Key) => apiClient.get<Result<any>>({ url: `/tool/gen/synchDb/${tableName}` });
//批量生成代码
const batchGenCode = (tables: string) => 
	apiClient.get<Result<any>>(
		{ 
			url: "/tool/gen/batchGenCode", 
			params: { tables: tables } 
		}
	);
//获取子表信息
const getChildrenTable = (tableName: string) => apiClient.get<TableInfo[]>({ url: `/tool/gen/children/${tableName}` });

//获取全部生成表信息
const allList = () => apiClient.get<TableInfo[]>({url:"/tool/gen/all"})

export default {
	pageList,
	getInfo,
	add,
	update,
	deleteById,
	dbList,
	columnList,
	importTable,
	preview,
	download,
	genCode,
	synchDb,
	batchGenCode,
	allList,
	getChildrenTable
};
